<?php

namespace App\Common\WX;


use App\Common\Log;
use App\Common\Sys;
use App\Common\WX\App\WxAppCommon;
use App\Model\t_wx_dialog;
use App\Model\t_wx_reply;
use App\X\EHelper;
use Carbon\Carbon;
use EasySwoole\WeChat\Bean\OfficialAccount\Message\News;
use EasySwoole\WeChat\Bean\OfficialAccount\Message\NewsItem;
use EasySwoole\WeChat\Bean\OfficialAccount\Message\RequestedReplyMsg;
use EasySwoole\WeChat\Bean\OfficialAccount\Message\Text;
use EasySwoole\WeChat\Bean\OfficialAccount\RequestMsg;
use XWX\Common\H;

class WxCenter extends _Base
{
    private $pub_wx_request;

    public function __construct(RequestMsg $req)
    {
        $this->pub_wx_request = $req;
    }

    /**
     * @return RequestMsg
     */
    public function funcGetWxReq()
    {
        return $this->pub_wx_request;
    }


    public function funcHandle()
    {
        $wx_req = $this->funcGetWxReq();


        if (EHelper::funcIsDev())
        {
            $this->funcAddDiglog($wx_req);
        }


        //根据type，进行分配到不同type，返回word
        $type = $wx_req->getMsgType();
        $class_name__wxcenter_fix = "WxHandle_{$type}";
        if ($type == 'event')
        {
            $event = $wx_req->getEvent();
            $event = H::funcStrToLower($event);
            $class_name__wxcenter_fix = "WxHandle_Event_{$event}";
        }


        $class_name__wxcenter = sprintf('App\Common\WX\Handle\%s', $class_name__wxcenter_fix);
        Log::funcLogDebug(__METHOD__, $class_name__wxcenter);
        if (!class_exists($class_name__wxcenter))
        {
            return null;
        }

        $app_wxcenter_x = new  $class_name__wxcenter();
        /** @var WxWord $word */
        $word = $app_wxcenter_x->funcHandle($wx_req);
        if (H::funcStrIsNullOrEmpty($word->content()))
        {
            return null;
        }


        Log::funcLogDebug(__METHOD__, "word->content={$word->content()}");
        Log::funcLogDebug(__METHOD__, "word->app={$word->app()}");
        Log::funcLogDebug(__METHOD__, "word->key={$word->key()}");
        Log::funcLogDebug(__METHOD__, "word->suffix={$word->suffix()}");


        $wx_form = $wx_req->getFromUserName();
        $x = $this->context()->funcGetX();
        //根据word进行回复
        EHelper::funcGo(function () use ($word, $type, $x, $wx_form)
        {
            $this->context()->funcSetX($x);

            try
            {
                //查找app，特殊处理
                $app = null;
                //App\Common\WX\App\test\text
                $class_name = sprintf('App\Common\WX\App\%s\%s', $word->app(), $type);
                Log::funcLogDebug(__METHOD__, $class_name);
                if (class_exists($class_name))
                {
                    Log::funcLogDebug(__METHOD__, 'app，特殊处理');
                    $app = new $class_name();
                }
                else
                {
                    //通用处理
                    $app = new WxAppCommon();
                }

                $app->funcHandle($word, $wx_form);
            }
            catch (\Exception $ex)
            {
                Log::funcLogErr(__METHOD__, $ex->getMessage());
            }
        });


        return null;
    }


    /**
     * 对话记录数据库
     * @param RequestMsg $wx_req
     * @throws \Exception
     */
    private function funcAddDiglog(RequestMsg $wx_req)
    {
        Log::funcLogDebug(__METHOD__, "对话记录数据库");

        $x = $this->context()->funcGetX();
        EHelper::funcGo(function () use ($wx_req, $x)
        {
            $this->context()->funcSetX($x);

            $entity_diglog = new t_wx_dialog();
            foreach ($wx_req->toArray() as $k => $v)
            {
                $entity_diglog->{$entity_diglog->funcGetName($k)} = $v;
            }
            $entity_diglog->wxdlg_CreateTime_date = Carbon::createFromTimestamp($entity_diglog->wxdlg_CreateTime);
            $entity_diglog->funcSetX($this->funcGetX());
            $entity_diglog->funcSetAutoID();

            $entity_diglog->funcInsert();
        });
    }
}